home *** CD-ROM | disk | FTP | other *** search
- {
- GPC demo program about strings being schemata (Extended Pascal feature).
- This is similar to schemademo.pas.
-
- Copyright (C) 1999-2001 Free Software Foundation, Inc.
-
- Author: Frank Heckenbach <frank@pascal.gnu.de>
-
- This program is free software; you can redistribute it and/or
- modify it under the terms of the GNU General Public License as
- published by the Free Software Foundation, version 2.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; see the file COPYING. If not, write to
- the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- Boston, MA 02111-1307, USA.
-
- As a special exception, if you incorporate even large parts of the
- code of this demo program into another program with substantially
- different functionality, this does not cause the other program to
- be covered by the GNU General Public License. This exception does
- not however invalidate any other reasons why it might be covered
- by the GNU General Public License.
- }
-
- program StringSchemaDemo;
-
- {
- A procedure taking a string (schema) parameter. A string is a built-in
- schema type with the following declaration:
-
- String (Capacity : Cardinal) = record
- Length : 0 .. Capacity;
- Chars : packed array [1 .. Capacity + 1] of Char
- end;
- }
- procedure Test (var s : String);
- begin
- Writeln ('The procedure was called with a string of capacity ', s.Capacity, ', length ', Length (s));
- Writeln ('and the value ''', s, '''.');
- Writeln;
- Writeln ('Now we append ''bar'' to the string. However, the routines know the');
- Writeln ('capacity of the string, so if appending to it would make it longer than');
- Writeln ('its capacity, they will not write beyond the capacity. This works simply');
- Writeln ('because strings are schemata, without any work required by the programmer.');
- Writeln;
- s := s + 'bar';
- Writeln ('The string has now a capacity of ', s.Capacity, ' (still), a length of ', Length (s));
- Writeln ('and the value ''', s, '''.');
- Writeln
- end;
-
- const
- Foo : String (10) = 'foo';
-
- var
- a : Integer;
- StrPtr : ^String;
-
- begin
- Test (Foo);
-
- repeat
- Write ('Enter the capacity for a string: ');
- Readln (a)
- until a >= 1;
-
- { A schema array declared in the statement part (GPC extension) }
- var t : String (a);
-
- Write ('Enter the contents of the string: ');
- Readln (t);
- Writeln;
- Test (t);
-
- Writeln ('Now we dynamically allocate a string of the same capacity.');
- New (StrPtr, a);
- Write ('Enter the contents of the new string: ');
- Readln (StrPtr^);
- Writeln;
- Test (StrPtr^);
- Dispose (StrPtr)
- end.
-